<template>
	<view>
		<uni-nav-bar dark title="实时数据" fixed :statusBar="true" left-icon="left" @clickLeft="onLeft">
			<block slot="right">
				<view class="city">
					<view @click="onEnd">
						结束
					</view>
				</view>
			</block>
		</uni-nav-bar>
		<view class="header_fen">
			<view class="item">
				{{mapList.length && mapList[0] ? mapList[0].teamName : ''}}
				<text>{{mapList.length && mapList[0] ? mapList[0].teamSubsectionScoreSumTotal : 0}}</text>
				<text>:</text>
				<text>{{mapList.length && mapList[1] ? mapList[1].teamSubsectionScoreSumTotal : 0}}</text>
				{{mapList.length && mapList[1] ? mapList[1].teamName : ''}}
			</view>
		</view>
		<view class="listfen">

			<uni-table ref="table" border stripe emptyText="暂无更多数据">
				<uni-tr>
					<uni-th width="30" align="center">球队</uni-th>
					<uni-th width="10" align="center" v-for="(item, key) in teamSubsectionScoreSum1">
						Q{{key}}
					</uni-th>
					<uni-th width="30" align="center">总分</uni-th>
				</uni-tr>
				<uni-tr>
					<uni-td align="center">
						{{oneName}}
					</uni-td>
					<uni-td align="center" v-for="(item, key) in teamSubsectionScoreSum1">
						{{item}}
					</uni-td>
					<uni-td align="center">
						{{mapList.length && mapList[0] ? mapList[0].teamSubsectionScoreSumTotal : 0}}
					</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-td align="center">
						{{twoName}}
					</uni-td>
					<uni-td v-for="(item, key) in teamSubsectionScoreSum2" align="center">
						{{item}}
					</uni-td>
					<uni-td align="center">
						{{mapList.length && mapList[1] ? mapList[1].teamSubsectionScoreSumTotal : 0}}
					</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		<view>
			<view style="padding: 30rpx 0;">
				<!-- <uni-segmented-control :values="items" style-type="text" active-color="#E96E40"
					@clickItem="onClickItem" /> -->
				<view class="dataBox-team">
					<view class="dataBox-team-item" :class="teamIndex==0? 'teamActive':''" @click="onClickItem(0)">
						<!-- <block v-if="teamIndex==0">
							<image v-if="info.teamOneLogo" :src="sercerImage+info.teamOneLogo"></image>
							<image v-else src="@/static/images/logo.jpg"></image>
						</block> -->
						<view class="dataBox-team-title">
							<view>{{oneTeam.teamName}}</view>
							<!-- <view>主 <text v-if="info.teamOneRanking">/ {{info.teamOneRanking}}</text></view> -->
						</view>
					</view>

					<view class="dataBox-team-item" :class="teamIndex==1? 'teamActive':''" @click="onClickItem(1)">
						<!-- <block v-if="teamIndex==1">
							<image v-if="info.teamTwoLogo" :src="sercerImage+info.teamTwoLogo"></image>
							<image v-else src="@/static/images/logo.jpg"></image>
						</block> -->
						<view class="dataBox-team-title">
							<view>{{twoTeam.teamName}}</view>
							<!-- <view>客 <text v-if="info.teamTwoRanking">/ {{info.teamTwoRanking}}</text></view> -->
						</view>
					</view>
				</view>
			</view>
			<view>
				<uni-table border v-if="downbox.length">
					<uni-tr>
						<uni-th width="140" align="center">球员</uni-th>

						<uni-th width="80" align="center" v-for="(item, key) in downbox">
							{{item.info}}
						</uni-th>
						<uni-th width="80" align="center">总分</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, ind) in userList" :key="ind">
						<uni-td align="left">
							<text class="code" style="margin-right: 20rpx;" @click="onshow(item.basketballPlayerId)">
								{{item.code}}
							</text>
							{{item.name}}
						</uni-td>

						<!-- <uni-td align="center" v-for="child in downbox">
							{{getFindFen(child.code, item.typeMapList)}}
						</uni-td> -->
						<uni-td align="center" v-for="(itemS, indS) in titleList" :key="indS">
							<text
								v-if="['罚球命中率','二分命中率','三分命中率','效率值','真实命中率'].includes(itemS.info) && item.name=='总计'"></text>
							<text v-else>{{getFindFen(itemS.code, item.typeMapList)}}</text>
						</uni-td>
						<uni-td align="center">{{item.typeScoreSum}}</uni-td>
					</uni-tr>
				</uni-table>
			</view>
		</view>

	</view>

</template>

<script>
	import {
		getrealTimeData,
		getrealTimeDataTeam,
		getdownBox,
		overTournament
	} from '@/api/record/index.js'
	export default {
		data() {
			return {
				id: '',
				info: {},
				oneTeam: {},
				twoTeam: {},
				mapList: [],
				teamSubsectionScoreSum1: {},
				teamSubsectionScoreSum2: {},
				downbox: [],
				items: [],
				userList: [],
				currentIndex: 0,
				oneName: '',
				twoName: '',
				teamIndex: 0,
				titleList: [],
			}
		},
		methods: {
			onshow(id) {
				uni.navigateTo({
					url: `/pages/record/operation?basketballPlayerId=${id}&tournamentId=${this.id}&typeList=${this.currentIndex}`
				})
			},
			onEnd() {
				uni.showModal({
					content: '确定结束比赛吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '提交中...'
							})
							overTournament({
								id: this.id
							}).then(res => {
								let seasonId = uni.getStorageSync('seasonId')
								uni.removeStorageSync('sourceList');
								uni.removeStorageSync('sourceTwo');
								uni.removeStorageSync('sourceOne');
								uni.removeStorageSync('fanGuiNumOne');
								uni.removeStorageSync('fanGuiNumTwo');
								uni.removeStorageSync('zanTingOne');
								uni.removeStorageSync('zanTingTwo');
								uni.removeStorageSync('fenData');
								uni.removeStorageSync('localId');
								uni.hideLoading();
								uni.reLaunch({
									url: '/pages/competition/home?id=' + seasonId
								})
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			onLeft() {
				uni.navigateBack()
			},
			onClickItem(val) {
				// let {
				// 	currentIndex
				// } = val
				// this.currentIndex = currentIndex
				// if (currentIndex == 0) {
				// 	this._getrealTimeDataTeam(this.oneTeam.id)
				// } else {
				// 	this._getrealTimeDataTeam(this.twoTeam.id)
				// }
				this.teamIndex = val
				if (val == 0) {
					this._getrealTimeDataTeam(this.oneTeam.id)
				} else {
					this._getrealTimeDataTeam(this.twoTeam.id)
				}
			},
			getFindFen(type, child) {
				console.log('type', type)
				console.log('child', child)
				// let item = child.find(data => data.type == type)
				// if (item) {
				// 	return item.typeCount
				// } else {
				// 	return 0
				// }
				let data = child.find((item) => item.type == type);
				if (data) {
					return [90, 91, 92, 93, 95].includes(type) ?
						`${data.typeCount ? (data.typeCount * 100).toFixed(0) + "%" : 0}` :
						data.typeCount;
				} else {
					return 0;
				}
			},
			_getrealTimeDataTeam(id) {
				getrealTimeDataTeam({
					tournamentId: this.id,
					basketballTeamId: id
				}).then(res => {
					this.userList = res.data
				})
			},
			_getrealTimeData() {
				let contestId = uni.getStorageSync('contestId')
				let organizationId = uni.getStorageSync('seasonId')
				getrealTimeData({
					tournamentId: this.id,
					contestId,
					organizationId
				}).then(res => {
					this.mapList = res.data.mapList || []
					this.teamSubsectionScoreSum1 = this.mapList.length && this.mapList[0] ? this.mapList[0]
						.teamSubsectionScoreSum : 0
					this.teamSubsectionScoreSum2 = this.mapList.length && this.mapList[1] ? this.mapList[1]
						.teamSubsectionScoreSum : 0
					this.oneName = this.mapList.length && this.mapList[0] ? this.mapList[0].teamName : ''
					this.twoName = this.mapList.length && this.mapList[1] ? this.mapList[1].teamName : ''
					this.items = [this.oneTeam.teamName, this.twoTeam.teamName]


				})
			}
		},
		onLoad({
			id
		}) {
			this.id = id;

			//获取球队信息
			let info = uni.getStorageSync('info');
			this.oneTeam = info.oneTeam;
			this.twoTeam = info.twoTeam;
			// this._getrealTimeDataTeam(this.oneTeam.id)
			// this._getrealTimeData()
			let contestId = uni.getStorageSync('contestId')
			getdownBox({
				contestId,
			}).then(res => {
				this.downbox = res.data

				let filterKeyMap =
					this.scoringWay == 0 ? ["一分命中率"] :
					this.scoringWay == 1 ? ["三分命中率"] : [];
				let list = res.data || [];
				console.log("list", filterKeyMap, list);
				list.filter((i) => {
					console.log("info", i.info, filterKeyMap.includes(i.info));
				});
				// console.log(
				//   "f",
				//   list.filter((i) => !filterKeyMap.includes(i.info))
				// );
				this.titleList = list.filter((i) => !filterKeyMap.includes(i.info));
			});
		},
		onShow() {
			this._getrealTimeDataTeam(this.oneTeam.id)
			this._getrealTimeData()
		}
	}
</script>

<style scoped lang="scss">
	.header_fen {
		display: flex;
		padding: 30rpx 20rpx;

		.item {
			display: flex;
			justify-content: center;
			width: 100%;
			align-items: center;
			font-weight: 800;

			text {
				font-weight: 800;
				font-size: 48rpx;

				&:nth-child(1) {
					margin: 0 20rpx;
				}

				&:nth-child(3) {
					margin: 0 20rpx;
				}
			}
		}
	}

	.code {
		display: inline-block;
		padding: 10rpx 20rpx;
		background-color: red;
		color: #fff;
		border-radius: 20rpx;
	}

	.dataBox-team {
		display: flex;
		align-items: center;
		padding: 10rpx 10rpx 0;
		background: #f3f3f3;
		position: relative;
		z-index: 10;

		.dataBox-team-item {
			width: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 16rpx;
			color: #666;

			image {
				width: 50rpx;
				height: 50rpx;
				margin-right: 10rpx;
				border-radius: 50%;
			}

			.dataBox-team-title {
				view:nth-child(1) {}

				view:nth-child(2) {
					color: #999;
					font-size: 22rpx;
				}
			}
		}

		.teamActive {
			background: #fff;
			border-radius: 40rpx 40rpx 0 0;
			color: #000 !important;
			font-weight: bold;
		}
	}
</style>